local huigu = fk.CreateSkill {
  name = "th_huigu",
}

huigu:addEffect(fk.CardUseFinished, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(huigu.name) and not player:isKongcheng() and target and target ~= player and data.card and (data.card.type == Card.TypeBasic or data.card:isCommonTrick()) then
      local targets = data:getAllTargets()
      local distances = table.filter(player.room.alive_players, function(p) return p:compareDistance(player, 1, "<=") end)
      if table.find(distances, function(e) return table.contains(targets, e) end) then
        return not target.dead and player:canUseTo(Fk:cloneCard(data.card.name), target, {bypass_distances = true, bypass_times = true})
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local hands = table.filter(player:getHandlyIds(), function(cid)
      local card = Fk:cloneCard(data.card.name)
      card.skillName = huigu.name
      card:addSubcard(cid)
      return player:canUseTo(card, target, {bypass_distances = true, bypass_times = true})
    end)
    local dat = room:askToCards(player, {
      pattern = tostring(Exppattern:new{ id = hands }),
      min_num = 1,
      max_num = 1,
      skill_name = huigu.name,
      prompt = "#th_huigu-invoke::" .. target.id .. ":" .. data.card.name,
      cancelable = true
    })
    if #dat ~= 0 then
      event:setCostData(self, { cards = dat })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    player.room:useVirtualCard(data.card.name, event:getCostData(self).cards, player, target, huigu.name, false)
  end,
})

Fk:loadTranslationTable{
  ["th_huigu"] = "回谷",
  [":th_huigu"] = "其他角色使用指定与你距离不大于1的角色为目标的基本牌或普通锦囊牌后，你可以将一张手牌当同名牌仅对其使用。",

  ["#th_huigu-invoke"] = "回谷：你可以将一张手牌当 %arg 对 %dest 使用",
}

return huigu